<?php

namespace app\common\controller;

use app\admin\library\Auth;
use think\Config;
use think\Controller;
use think\Db;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Model;
use think\Request;
use think\Session;
use fast\Tree;
use think\Validate;

/**
 * 后台控制器基类
 */
class Backend extends Controller
{

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    /**
     * 布局模板
     * @var string
     */
    protected $layout = 'default';

    /**
     * 权限控制类
     * @var Auth
     */
    protected $auth = null;

    /**
     * 模型对象
     * @var \think\Model
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'id';

    /**
     * 是否是关联查询
     */
    protected $relationSearch = false;

    /**
     * 是否开启数据限制
     * 支持auth/personal
     * 表示按权限判断/仅限个人
     * 默认为禁用,若启用请务必保证表中存在admin_id字段
     */
    protected $dataLimit = false;

    /**
     * 数据限制字段
     */
    protected $dataLimitField = 'admin_id';

    /**
     * 数据限制开启时自动填充限制字段值
     */
    protected $dataLimitFieldAutoFill = true;

    /**
     * 是否开启Validate验证
     */
    protected $modelValidate = false;

    /**
     * 是否开启模型场景验证
     */
    protected $modelSceneValidate = false;

    /**
     * Multi方法可批量修改的字段
     */
    protected $multiFields = 'status';

    /**
     * Selectpage可显示的字段
     */
    protected $selectpageFields = '*';

    /**
     * 前台提交过来,需要排除的字段数据
     */
    protected $excludeFields = "";

    /**
     * 导入文件首行类型
     * 支持comment/name
     * 表示注释或字段名
     */
    protected $importHeadType = 'comment';

    /**
     * 引入后台控制器的traits
     */
    use \app\admin\library\traits\Backend;

    public function _initialize()
    {
        $modulename = $this->request->module();
        $controllername = Loader::parseName($this->request->controller());
        $actionname = strtolower($this->request->action());

        $path = str_replace('.', '/', $controllername) . '/' . $actionname;

        // 定义是否Addtabs请求
        !defined('IS_ADDTABS') && define('IS_ADDTABS', input("addtabs") ? true : false);

        // 定义是否Dialog请求
        !defined('IS_DIALOG') && define('IS_DIALOG', input("dialog") ? true : false);

        // 定义是否AJAX请求
        !defined('IS_AJAX') && define('IS_AJAX', $this->request->isAjax());

        // 检测IP是否允许
        check_ip_allowed();

        $this->auth = Auth::instance();

        // 设置当前请求的URI
        $this->auth->setRequestUri($path);
        // 检测是否需要验证登录
        if (!$this->auth->match($this->noNeedLogin)) {
            //检测是否登录
            if (!$this->auth->isLogin()) {
                Hook::listen('admin_nologin', $this);
                $url = Session::get('referer');
                $url = $url ? $url : $this->request->url();
                if ($url == '/') {
                    $this->redirect('index/login', [], 302, ['referer' => $url]);
                    exit;
                }
                $this->error(__('Please login first'), url('index/login', ['url' => $url]));
            }
            // 判断是否需要验证权限
            if (!$this->auth->match($this->noNeedRight)) {
                // 判断控制器和方法是否有对应权限
                if (!$this->auth->check($path)) {
                    Hook::listen('admin_nopermission', $this);
                    $this->error(__('You have no permission'), '');
                }
            }
        }

        // 非选项卡时重定向
        if (!$this->request->isPost() && !IS_AJAX && !IS_ADDTABS && !IS_DIALOG && input("ref") == 'addtabs') {
            $url = preg_replace_callback("/([\?|&]+)ref=addtabs(&?)/i", function ($matches) {
                return $matches[2] == '&' ? $matches[1] : '';
            }, $this->request->url());
            if (Config::get('url_domain_deploy')) {
                if (stripos($url, $this->request->server('SCRIPT_NAME')) === 0) {
                    $url = substr($url, strlen($this->request->server('SCRIPT_NAME')));
                }
                $url = url($url, '', false);
            }
            $this->redirect('index/index', [], 302, ['referer' => $url]);
            exit;
        }

        // 设置面包屑导航数据
        $breadcrumb = [];
        if (!IS_DIALOG && !config('fastadmin.multiplenav') && config('fastadmin.breadcrumb')) {
            $breadcrumb = $this->auth->getBreadCrumb($path);
            array_pop($breadcrumb);
        }
        $this->view->breadcrumb = $breadcrumb;

        // 如果有使用模板布局
        if ($this->layout) {
            $this->view->engine->layout('layout/' . $this->layout);
        }

        // 语言检测
        $lang = strip_tags($this->request->langset());

        $site = Config::get("site");

        $upload = \app\common\model\Config::upload();

        // 上传信息配置后
        Hook::listen("upload_config_init", $upload);

        // 配置信息
        $config = [
            'site'           => array_intersect_key($site, array_flip(['name', 'indexurl', 'cdnurl', 'version', 'timezone', 'languages'])),
            'upload'         => $upload,
            'modulename'     => $modulename,
            'controllername' => $controllername,
            'actionname'     => $actionname,
            'jsname'         => 'backend/' . str_replace('.', '/', $controllername),
            'moduleurl'      => rtrim(url("/{$modulename}", '', false), '/'),
            'language'       => $lang,
            'referer'        => Session::get("referer")
        ];
        $config = array_merge($config, Config::get("view_replace_str"));

        Config::set('upload', array_merge(Config::get('upload'), $upload));

        // 配置信息后
        Hook::listen("config_init", $config);
        //加载当前控制器语言包
        $this->loadlang($controllername);
        //渲染站点配置
        $this->assign('site', $site);
        //渲染配置信息
        $this->assign('config', $config);
        //渲染权限对象
        $this->assign('auth', $this->auth);
        //渲染管理员对象
        $this->assign('admin', Session::get('admin'));
    }

    /**
     * 加载语言文件
     * @param string $name
     */
    protected function loadlang($name)
    {
        $name = Loader::parseName($name);
        Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php');
    }

    /**
     * 渲染配置信息
     * @param mixed $name  键名或数组
     * @param mixed $value 值
     */
    protected function assignconfig($name, $value = '')
    {
        $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
    }

    /**
     * 生成查询所需要的条件,排序方式
     * @param mixed   $searchfields   快速查询的字段
     * @param boolean $relationSearch 是否关联查询
     * @return array
     */
    protected function buildparams($searchfields = null, $relationSearch = null)
    {
        $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
        $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
        $search = $this->request->get("search", '');
        $filter = $this->request->get("filter", '');
        $op = $this->request->get("op", '', 'trim');
        $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
        $order = $this->request->get("order", "DESC");
        $offset = $this->request->get("offset/d", 0);
        $limit = $this->request->get("limit/d", 999999);
        //新增自动计算页码
        $page = $limit ? intval($offset / $limit) + 1 : 1;
        if ($this->request->has("page")) {
            $page = $this->request->get("page/d", 1);
        }
        $this->request->get([config('paginate.var_page') => $page]);
        $filter = (array)json_decode($filter, true);
        $op = (array)json_decode($op, true);
        $filter = $filter ? $filter : [];
        $where = [];
        $alias = [];
        $bind = [];
        $name = '';
        $aliasName = '';
        if (!empty($this->model) && $this->relationSearch) {
            $name = $this->model->getTable();
            $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
            $aliasName = $alias[$name] . '.';
        }
        $sortArr = explode(',', $sort);
        foreach ($sortArr as $index => & $item) {
            $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
        }
        unset($item);
        $sort = implode(',', $sortArr);
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
        }
        if ($search) {
            $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
            foreach ($searcharr as $k => &$v) {
                $v = stripos($v, ".") === false ? $aliasName . $v : $v;
            }
            unset($v);
            $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
        }
        $index = 0;
        foreach ($filter as $k => $v) {
            if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
                continue;
            }
            $sym = isset($op[$k]) ? $op[$k] : '=';
            if (stripos($k, ".") === false) {
                $k = $aliasName . $k;
            }
            $v = !is_array($v) ? trim($v) : $v;
            $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
            //null和空字符串特殊处理
            if (!is_array($v)) {
                if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
                    $sym = strtoupper($v);
                }
                if (in_array($v, ['""', "''"])) {
                    $v = '';
                    $sym = '=';
                }
            }

            switch ($sym) {
                case '=':
                case '<>':
                    $where[] = [$k, $sym, (string)$v];
                    break;
                case 'LIKE':
                case 'NOT LIKE':
                case 'LIKE %...%':
                case 'NOT LIKE %...%':
                    $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
                    break;
                case '>':
                case '>=':
                case '<':
                case '<=':
                    $where[] = [$k, $sym, intval($v)];
                    break;
                case 'FINDIN':
                case 'FINDINSET':
                case 'FIND_IN_SET':
                    $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
                    $findArr = array_values($v);
                    foreach ($findArr as $idx => $item) {
                        $bindName = "item_" . $index . "_" . $idx;
                        $bind[$bindName] = $item;
                        $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
                    }
                    break;
                case 'IN':
                case 'IN(...)':
                case 'NOT IN':
                case 'NOT IN(...)':
                    $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
                    break;
                case 'BETWEEN':
                case 'NOT BETWEEN':
                    $arr = array_slice(explode(',', $v), 0, 2);
                    if (stripos($v, ',') === false || !array_filter($arr)) {
                        continue 2;
                    }
                    //当出现一边为空时改变操作符
                    if ($arr[0] === '') {
                        $sym = $sym == 'BETWEEN' ? '<=' : '>';
                        $arr = $arr[1];
                    } elseif ($arr[1] === '') {
                        $sym = $sym == 'BETWEEN' ? '>=' : '<';
                        $arr = $arr[0];
                    }
                    $where[] = [$k, $sym, $arr];
                    break;
                case 'RANGE':
                case 'NOT RANGE':
                    $v = str_replace(' - ', ',', $v);
                    $arr = array_slice(explode(',', $v), 0, 2);
                    if (stripos($v, ',') === false || !array_filter($arr)) {
                        continue 2;
                    }
                    //当出现一边为空时改变操作符
                    if ($arr[0] === '') {
                        $sym = $sym == 'RANGE' ? '<=' : '>';
                        $arr = $arr[1];
                    } elseif ($arr[1] === '') {
                        $sym = $sym == 'RANGE' ? '>=' : '<';
                        $arr = $arr[0];
                    }
                    $tableArr = explode('.', $k);
                    if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
                        //修复关联模型下时间无法搜索的BUG
                        $relation = Loader::parseName($tableArr[0], 1, false);
                        $alias[$this->model->$relation()->getTable()] = $tableArr[0];
                    }
                    $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
                    break;
                case 'NULL':
                case 'IS NULL':
                case 'NOT NULL':
                case 'IS NOT NULL':
                    $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
                    break;
                default:
                    break;
            }
            $index++;
        }
        if (!empty($this->model)) {
            $this->model->alias($alias);
        }
        $model = $this->model;
        $where = function ($query) use ($where, $alias, $bind, &$model) {
            if (!empty($model)) {
                $model->alias($alias);
                $model->bind($bind);
            }
            foreach ($where as $k => $v) {
                if (is_array($v)) {
                    call_user_func_array([$query, 'where'], $v);
                } else {
                    $query->where($v);
                }
            }
        };
        return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
    }

    /**
     * 获取数据限制的管理员ID
     * 禁用数据限制时返回的是null
     * @return mixed
     */
    protected function getDataLimitAdminIds()
    {
        if (!$this->dataLimit) {
            return null;
        }
        if ($this->auth->isSuperAdmin()) {
            return null;
        }
        $adminIds = [];
        if (in_array($this->dataLimit, ['auth', 'personal'])) {
            $adminIds = $this->dataLimit == 'auth' ? $this->auth->getChildrenAdminIds(true) : [$this->auth->id];
        }
        return $adminIds;
    }

    /**
     * Selectpage的实现方法
     *
     * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可
     * 这里示例了所有的参数，所以比较复杂，实现上自己实现只需简单的几行即可
     *
     */
    protected function selectpage()
    {
        //设置过滤方法
        $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);

        //搜索关键词,客户端输入以空格分开,这里接收为数组
        $word = (array)$this->request->request("q_word/a");
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");
        //搜索条件
        $andor = $this->request->request("andOr", "and", "strtoupper");
        //排序方式
        $orderby = (array)$this->request->request("orderBy/a");
        //显示的字段
        $field = $this->request->request("showField");
        //主键
        $primarykey = $this->request->request("keyField");
        //主键值
        $primaryvalue = $this->request->request("keyValue");
        //搜索字段
        $searchfield = (array)$this->request->request("searchField/a");
        //自定义搜索条件
        $custom = (array)$this->request->request("custom/a");
        //是否返回树形结构
        $istree = $this->request->request("isTree", 0);
        $ishtml = $this->request->request("isHtml", 0);
        if ($istree) {
            $word = [];
            $pagesize = 999999;
        }
        $order = [];
        foreach ($orderby as $k => $v) {
            $order[$v[0]] = $v[1];
        }
        $field = $field ? $field : 'name';

        //如果有primaryvalue,说明当前是初始化传值
        if ($primaryvalue !== null) {
            $where = [$primarykey => ['in', $primaryvalue]];
            $pagesize = 999999;
        } else {
            $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
                $logic = $andor == 'AND' ? '&' : '|';
                $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
                $searchfield = str_replace(',', $logic, $searchfield);
                $word = array_filter(array_unique($word));
                if (count($word) == 1) {
                    $query->where($searchfield, "like", "%" . reset($word) . "%");
                } else {
                    $query->where(function ($query) use ($word, $searchfield) {
                        foreach ($word as $index => $item) {
                            $query->whereOr(function ($query) use ($item, $searchfield) {
                                $query->where($searchfield, "like", "%{$item}%");
                            });
                        }
                    });
                }
                if ($custom && is_array($custom)) {
                    foreach ($custom as $k => $v) {
                        if (is_array($v) && 2 == count($v)) {
                            $query->where($k, trim($v[0]), $v[1]);
                        } else {
                            $query->where($k, '=', $v);
                        }
                    }
                }
            };
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $list = [];
        $total = $this->model->where($where)->count();
        if ($total > 0) {
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }

            $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);

            //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
            if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
                $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
                //修复自定义data-primary-key为字符串内容时，给排序字段添加上引号
                $primaryvalue = array_map(function ($value) {
                    return '\'' . $value . '\'';
                }, $primaryvalue);

                $primaryvalue = implode(',', $primaryvalue);

                $this->model->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
            } else {
                $this->model->order($order);
            }

            $datalist = $this->model->where($where)
                ->page($page, $pagesize)
                ->select();
            foreach ($datalist as $index => $item) {
                unset($item['password'], $item['salt']);
                if ($this->selectpageFields == '*') {
                    if(isset($item['admin_id'])){
                        $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                    }
                    $result = [
                        $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
                        $field      => isset($item[$field]) ? $item[$field] : '',
                        'email'      => isset($item['email']) ? $item['email'] : '',
                        'url'      => isset($item['url']) ? $item['url'] : '',
                        'nickname' => isset($admin['nickname']) ? $admin['nickname'] : '',
                        'numbername' => isset($item['numbername']) ? $item['numbername'] : '',
                        'number' => isset($item['number']) ? $item['number'] : '',
                        'regions_id' => isset($item['regions_id']) ? $item['regions_id'] : '',
                        'country' => isset($item['country']) ? $item['country'] : '',
                        'disk' => isset($item['disk']) ? $item['disk'] : '',
                    ];
                } else {
                    $result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array)$item), array_flip($fields));
                }
                if (isset($result['email']) && isset($result['numbername'])) {
                    $result['numbername_email'] = '邮箱：'.$result['email'].'，国家：'.$result['numbername'];
                }
                $result['pid'] = isset($item['pid']) ? $item['pid'] : (isset($item['parent_id']) ? $item['parent_id'] : 0);
                $list[] = $result;
            }
            if ($istree && !$primaryvalue) {
                $tree = Tree::instance();
                $tree->init(collection($list)->toArray(), 'pid');
                $list = $tree->getTreeList($tree->getTreeArray(0), $field);
                if (!$ishtml) {
                    foreach ($list as &$item) {
                        $item = str_replace('&nbsp;', ' ', $item);
                    }
                    unset($item);
                }
            }
        }
        //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
        return json(['list' => $list, 'total' => $total]);
    }

    /**
     * 刷新Token
     */
    protected function token()
    {
        $token = $this->request->param('__token__');

        //验证Token
        if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
            $this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]);
        }

        //刷新Token
        $this->request->token();
    }

    /**
     * 检测域名是否正确
     */
    protected function firstLevelDomainName($name)
    {
        $return['status'] = false;
        $return['msg'] = '参数错误';
        $return['data'] = $name;

        if(strpos($name,'http://') !== false){
            $return['msg'] = '无需填写http';
            return $return;
        }elseif(strpos($name,'https://') !== false){
            $return['msg'] = '无需填写https';
            return $return;
        }else{
            $regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\/|\;|\'|\`|\-|\=|\\\|\|/";
            $regexreturn = preg_match($regex,$name);
            if($regexreturn > 0){
                $return['msg'] = '请填写正确格式的一级域名';
                return $return;
            }
                $return['msg'] = '成功';
                $return['status'] = true;
                return $return;
        }
    }

    /**
     * 添加自定义操作日志
     * type 1 落地页建设,网站管理
     * type 2 域名操作
     * type 3 报表管理
     * type 4 账户管理
     * operation 1 新增
     * operation 2 修改
     * operation 3 具体修改
     *
     */
    protected function add_operation_log($type,$operation_id,$operation,$money=null)
    {
        $request = Request::instance();
        $data['type'] = 1;
        $data['admin_id'] = $this->auth->id;
        $data['createtime'] = time();
        $data['ip'] = $request->ip();
        $data['type'] = $type;
        $data['operation_id'] = $operation_id;
        switch ($type) {
            case 1:
                $data['title'] = '网站管理';
                if($operation == 1){
                    $data['content'] = '新增网站管理';
                }elseif ($operation == 2){
                    $data['content'] = '进行网站修改操作';
                }elseif ($operation == 3){
                    $data['content'] = '落地页建设状态修改成已完毕';
                }elseif ($operation == 4){
                    $data['content'] = '落地页建设状态修改成未完毕';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 2:
                $data['title'] = '域名';
                if($operation == 1){
                    $data['content'] = '新增域名';
                }elseif ($operation == 2){
                    $data['content'] = '进行域名修改操作';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 3:
                $data['title'] = '报表管理';
                if($operation == 1){
                    $data['content'] = '新增报表';
                }elseif ($operation == 2){
                    $data['content'] = '进行报表修改操作';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 5:
                $data['title'] = '报表管理充值记录';
                if($operation == 1){
                    $data['content'] = '新增充值金额为'.$money;
                }elseif ($operation == 2){
                    $data['content'] = '修改充值金额为'.$money;
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 6:
                $data['title'] = '报表日汇总记录';
                if($operation == 1){
                    $data['content'] = '新增日汇总记录';
                }elseif ($operation == 2){
                    $data['content'] = '修改日汇总记录';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 7:
                $data['title'] = '店铺收款明细记录';
                if($operation == 1){
                    $data['content'] = '修改今日收款金额为'.$money;
                }elseif ($operation == 2){
                    $data['content'] = '修改今日退款金额为'.$money;
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 8:
                $data['title'] = '转移店铺管理者或网站归属人';
                if($operation == 1){
                    $data['content'] = '修改店铺管理者id为'.$money;
                }
                break;
            default:
                return false;
        }
        $return = Db::name('fedition_log')->insert($data);
        return $return;
    }

    /**
     * 添加自定义操作日志
     * type 1 虚拟机账户管理
     * type 2 虚拟机申请账户
     * operation 1 新增
     * operation 2 修改
     * operation 3 具体修改
     *
     */
    protected function add_virtualmachine_log($type,$operation_id,$operation,$money=null)
    {
        $request = Request::instance();
        $data['type'] = 1;
        $data['admin_id'] = $this->auth->id;
        $data['createtime'] = time();
        $data['ip'] = $request->ip();
        $data['type'] = $type;
        $data['operation_id'] = $operation_id;
        switch ($type) {
            case 1:
                $data['title'] = '创建虚拟机账户';
                if($operation == 1){
                    $data['content'] = '创建虚拟机账户';
                }elseif ($operation == 2){
                    $data['content'] = '修改虚拟机账户';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
//            case 2:
//                $data['title'] = '申请广告账户';
//                if($operation == 1){
//                    $data['content'] = '新增广告账户申请';
//                }elseif ($operation == 2){
//                    $data['content'] = '修改广告账户申请';
//                }elseif ($operation == 3){
//                    $data['content'] = '申请通过';
//                }else{
//                    $data['content'] = '进行其他操作';
//                }
//                break;
            case 3:
                $data['title'] = '广告账户申请成功';
                if($operation == 1){
                    $data['content'] = '广告账户申请成功';
                }elseif ($operation == 2){
                    $data['content'] = '广告账户申请失败';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 4:
                $data['title'] = '广告账户';
                if($operation == 1){
                    $data['content'] = '新增广告账户';
                }elseif ($operation == 2){
                    $data['content'] = '进行广告账户修改操作';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 5:
                $data['title'] = 'P卡账户申请成功';
                if($operation == 1){
                    $data['content'] = 'P卡账户申请成功';
                }elseif ($operation == 2){
                    $data['content'] = 'P卡账户申请失败';
                }elseif ($operation == 3){
                    $data['content'] = '强制拒绝申请，账户数据回退';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 6:
                $data['title'] = '账户绑定P卡成功';
                if($operation == 1){
                    $data['content'] = '账户绑定P卡成功';
                }elseif ($operation == 2){
                    $data['content'] = '账户解除P卡绑定';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            default:
                return false;
        }
        $return = Db::name('virtualmachine_log')->insert($data);
        return $return;
    }

    /**
     * 添加自定义操作日志
     * type 1 虚拟机账户管理
     * type 2 虚拟机申请账户
     * operation 1 新增
     * operation 2 修改
     * operation 3 具体修改
     *
     */
    protected function add_pcardlog_log($type,$operation_id,$operation,$money=null)
    {
        $request = Request::instance();
        $data['type'] = 1;
        $data['admin_id'] = $this->auth->id;
        $data['createtime'] = time();
        $data['ip'] = $request->ip();
        $data['type'] = $type;
        $data['operation_id'] = $operation_id;
        switch ($type) {
            case 1:
                $data['title'] = '创建P卡账户';
                if($operation == 1){
                    $data['content'] = '创建P卡账户';
                }elseif ($operation == 2){
                    $data['content'] = '修改P卡账户';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            case 2:
                $data['title'] = '申请P卡账户';
                if($operation == 1){
                    $data['content'] = '新增P卡账户申请';
                }elseif ($operation == 2){
                    $data['content'] = '修改P卡账户申请';
                }elseif ($operation == 3){
                    $data['content'] = '申请通过';
                }elseif ($operation == 4){
                    $data['content'] = '强制拒绝申请，账户数据回退';
                }else{
                    $data['content'] = '进行其他操作';
                }
                break;
            default:
                return false;
        }
        $return = Db::name('virtualmachine_pcardlog')->insert($data);
        return $return;
    }

    /**
     * 绑定域名后 修改域名状态
     */
    protected function domain_usage_status($domain,$olddomain)
    {
        if(!empty($domain) && !empty($olddomain) && $olddomain !== $domain){
            Db::name('dns_list')->where('id',$olddomain)->update(
                [
                    'usage_status' => 'normal'
                ]
            );
            $dns = Db::name('dns_list')->where('id',$domain)->update(
                [
                    'usage_status' => 'hidden'
                ]
            );
            return $dns;
        }
        if(!empty($domain)){
            $dns = Db::name('dns_list')->where('id',$domain)->update(
                [
                    'usage_status' => 'hidden'
                ]
            );
            return $dns;
        }
    }

    /**
     * 判断域名是否被使用
     */
    protected function domain_judge($domain)
    {
        $website = Db::name('fedition_website')->where('domain',$domain)->find();
        if($website){
            Db::name('dns_list')->where('id',$domain)->update(
                [
                    'usage_status' => 'hidden'
                ]
            );
            return false;
        }else{
            return true;
        }
    }

    /**
     * 绑定域名后 修改域名状态
     */
    protected function account_usage_status($domain,$olddomain)
    {
        if(!empty($domain) && !empty($olddomain) && $olddomain !== $domain){
            Db::name('dns_list')->where('id',$olddomain)->update(
                [
                    'account_usage_status' => 'normal'
                ]
            );
            $dns = Db::name('dns_list')->where('id',$domain)->update(
                [
                    'account_usage_status' => 'hidden'
                ]
            );
            return $dns;
        }
        if(!empty($domain)){
            $dns = Db::name('dns_list')->where('id',$domain)->update(
                [
                    'account_usage_status' => 'hidden'
                ]
            );
            return $dns;
        }
    }

    /**
     * 判断域名是否被使用
     */
    protected function account_domain_judge($domain)
    {
        $website = Db::name('fedition_account')->where('domain',$domain)->find();
        if($website){
            Db::name('dns_list')->where('id',$domain)->update(
                [
                    'account_usage_status' => 'hidden'
                ]
            );
            return false;
        }else{
            return true;
        }
    }

    /**
     * 如果有收款账户则填入
     */
    protected function paymentinfo($paymentinfo,$id)
    {
        if(!empty($paymentinfo)){
            $paymentinfo = explode(',',$paymentinfo);
            $paymenttypes = array();
            foreach ($paymentinfo as $k=>$v){
                $accountpay = Db::name('fedition_accountpay')->where('id',$v)->find();
                if($accountpay){
                    $collectiontype = Db::name('fedition_collectiontype')->where('id',$accountpay['collection_type'])->find();
                    $arr['createtime'] = time();
                    $arr['updatetime'] = time();
                    $arr['accountpay_id'] = $v;
                    $arr['website_id'] = $id;
                    $website = Db::name('fedition_website')->where('id',$id)->find();
                    if($website){
                        $arr['accounturl_id'] = $website['account_address'];
                    }
                    $arr['collectiontype_id'] = $accountpay['collection_type'];
                    Db::name('fedition_websitepaytype')->insert($arr);
                    $paymenttypes[] = $collectiontype['name'];
                }
            }
            $data = array();
            if(!empty($paymenttypes)){
                $data['paymenttype'] = implode(',',array_unique($paymenttypes));
            }
            Db::name('fedition_website')->where('id',$id)->update($data);
        }
    }

    /**
     * 识别国家
     */
    protected function add_numbername($type)
    {
        if (!empty($type)) {
            switch ($type) {
                case 'US':
                    $data['numbername'] = 'US(美国)';
                    $data['country'] = '美国';
                    break;
                case 'EU':
                    $data['numbername'] = 'EU(欧盟)';
                    $data['country'] = '欧盟';
                    break;
                case 'CA':
                    $data['numbername'] = 'CA(加拿大)';
                    $data['country'] = '加拿大';
                    break;
                case 'AU':
                    $data['numbername'] = 'AU(澳大利亚)';
                    $data['country'] = '澳大利亚';
                    break;
                case 'KR':
                    $data['numbername'] = 'KR(韩国)';
                    $data['country'] = '韩国';
                    break;
                case 'DE':
                    $data['numbername'] = 'DE(德国)';
                    $data['country'] = '德国';
                    break;
                default:
                    $data['numbername'] = '未知';
                    $data['country'] = '未知';
            }
        } else {
            $data['numbername'] = '';
            $data['country'] = '';
        }
        return $data;
    }
}
